package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.global.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
    //本地磁盘
//    @PostMapping("/upload")
//    public Result upload(String name,Integer age,MultipartFile file ) throws IOException {
//        log.info("接收到的参数值:{},{},{}",name,age,file);
//        //获取文件名
//        String originalFilename = file.getOriginalFilename();
//        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newfilename = UUID.randomUUID().toString()+extension;
//        //保存文件
//        file.transferTo(new File("D:/images/"+newfilename));
//        return Result.success();
//    }
    //oss文件上穿
    @Autowired
    private AliyunOSSOperator ossOperator;
    @PostMapping("/upload")
    public Result upload( MultipartFile file) throws Exception {
        log.info("文件上转 {}",file.getOriginalFilename());
        //文件交给oss
        String url = ossOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传OSS：url:{}",url);

        return  Result.success(url);
    }
}
